package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.User;
import be.kuleuven.cs.mop.domain.model.UserType;
import be.kuleuven.cs.mop.gui.components.ActionButton;
import be.kuleuven.cs.mop.gui.components.ComboSelector;
import be.kuleuven.cs.mop.gui.components.TextInput;
import be.kuleuven.cs.mop.gui.util.SwingUtils;



/**
 * {@link ActionDialog} for the "Create User" use case
 */
public class CreateUserDialog extends ActionDialog {
	private static final long serialVersionUID = 1L;
	
	private TextInput				name	= new TextInput();
	private ComboSelector<UserType>	type;
	
	
	public CreateUserDialog(final Frame parent, final Controllers controllers) {
		super(parent, "Create User", controllers);
		final GridBagConstraints gbc = SwingUtils.getGBC();
		
		try {
			type = new ComboSelector<UserType>(
					getControllers().getControllerUsers().getUserTypes().toArray(
							new UserType[0]));
		} catch (final InvalidCurrentUserException e) {
			log(e);
			dispose();
			return;
		}
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		add(name, gbc);
		
		gbc.gridy	= 1;
		add(type, gbc);
		
		gbc.gridy	= 2;
		add(new ActionButton("Create", new Dimension(150, 32), this), gbc);
		
		display();
	}
	
	
	
	@Override
	protected void handle(final ActionEvent event)
			throws InvalidCurrentUserException, TaskManagerException {
		final User user = getControllers().getControllerUsers().createUser(
				name.getText(), type.getSelection());
		log("Created user '%s'", user.getName());
		dispose();
	}
	
}